package dal

import (
	"context"

	"gorm.io/gorm"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/errors"
	"lianzi_gos/pkg/util"
)

// Get goods attr storage instance
func GetGoodsAttrDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.GoodsAttr))
}

// 产品属性表
type GoodsAttr struct {
	DB *gorm.DB
}

// Query goods attrs from the database based on the provided parameters and options.
func (a *GoodsAttr) Query(ctx context.Context, params schema.GoodsAttrQueryParam, opts ...schema.GoodsAttrQueryOptions) (*schema.GoodsAttrQueryResult, error) {
	var opt schema.GoodsAttrQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	db := GetGoodsAttrDB(ctx, a.DB)

	if v := params.Status; len(v) > 0 {
		db = db.Where("status = ?", v)
	}
	if v := params.AttrId; len(v) > 0 {
		db = db.Where("id = ?", v)
	}
	if v := params.Types; len(v) > 0 {
		if v == "one" {
			if n := params.Name; len(n) > 0 {
				db = db.Where("name LIKE ?", "%"+n+"%")
			}
		}
	}
	if params.Source == 5 {
		//db = db.Where("code = ?", params.Code)
	} else {
		db = db.Where("code = ?", params.Code)
	}

	var list schema.GoodsAttrs
	pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	queryResult := &schema.GoodsAttrQueryResult{
		PageResult: pageResult,
		Data:       list,
	}
	return queryResult, nil
}

// Query goods attrs from the database based on the provided parameters and options.
func (a *GoodsAttr) QueryAdmin(ctx context.Context, gid string) []*schema.GoodsAttr {

	db := GetGoodsAttrDB(ctx, a.DB)

	var list schema.GoodsAttrs
	//item := new(schema.GoodsAttr)
	db.Where("gid = ?", gid).Find(&list)

	return list
}

// Get the specified goods attr from the database.
func (a *GoodsAttr) Get(ctx context.Context, id string, opts ...schema.GoodsAttrQueryOptions) (*schema.GoodsAttr, error) {
	var opt schema.GoodsAttrQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	item := new(schema.GoodsAttr)
	ok, err := util.FindOne(ctx, GetGoodsAttrDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

// Exist checks if the specified goods attr exists in the database.
func (a *GoodsAttr) Exists(ctx context.Context, id string) (bool, error) {
	ok, err := util.Exists(ctx, GetGoodsAttrDB(ctx, a.DB).Where("id=?", id))
	return ok, errors.WithStack(err)
}

// Create a new goods attr.
func (a *GoodsAttr) Create(ctx context.Context, item *schema.GoodsAttr) error {
	result := GetGoodsAttrDB(ctx, a.DB).Create(item)
	return errors.WithStack(result.Error)
}

// Update the specified goods attr in the database.
func (a *GoodsAttr) Update(ctx context.Context, item *schema.GoodsAttr) error {
	result := GetGoodsAttrDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
	return errors.WithStack(result.Error)
}

// Update the specified goods attr in the database.
func (a *GoodsAttr) UpdatePrice(ctx context.Context, item *schema.GoodsAttr) error {
	result := GetGoodsAttrDB(ctx, a.DB).Where("id=?", item.ID).Update("price", item.Price)
	return errors.WithStack(result.Error)
}

// Update the specified goods attr in the database.
func (a *GoodsAttr) UpdateStatus(ctx context.Context, ids []string) error {
	item := new(schema.GoodsAttr)
	item.Status = "enabled"
	result := GetGoodsAttrDB(ctx, a.DB).Where("id IN ?", ids).Updates(item)
	return errors.WithStack(result.Error)
}

// Update the specified goods attr in the database.
func (a *GoodsAttr) UpdateOneStatus(ctx context.Context, id, status string) error {
	result := GetGoodsAttrDB(ctx, a.DB).Where("id = ?", id).Update("status", status)
	return errors.WithStack(result.Error)
}

// Delete the specified goods attr from the database.
func (a *GoodsAttr) Delete(ctx context.Context, id string) error {
	result := GetGoodsAttrDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.GoodsAttr))
	return errors.WithStack(result.Error)
}
